package com.example.controller;

import com.example.domain.Result;
import com.example.domain.Account;
import com.example.service.LoginServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    LoginServiceImpl loginService;

    @PostMapping
    @ResponseBody
    public Result checkCode(HttpServletRequest request, @RequestBody Account account){
        Result r = loginService.check(account);
        Boolean flag = r.getFlag();
        if(flag){
            HttpSession session = request.getSession();
            session.setAttribute("account",account.getEmail());
            session.setMaxInactiveInterval(30 * 24 * 60);
            return new Result(flag, r.getData());
        }else {
            return new Result(flag, r.getMsg());
        }
    }
}
